package com.dubbo.common.exception;

import com.dubbo.common.ResponseUtil;
import org.apache.log4j.Logger;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/*import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.session.UnknownSessionException;*/

/**
 * 异常工具
 * @author chenwei
 * @date 2018年12月11日 下午5:07:03
 */
public class ExceptionUtils {
	private static Logger logger = Logger.getLogger(ExceptionUtils.class);
	private static Map<String, Object> map = null;
	/**
	 * 根据异常响应友好提示数据
	 * 
	 * @author chenwei
	 * @date 2018年12月11日 下午5:34:34
	 * @param ex 异常
	 * @param response 响应域对象
	 */
	public static void filterExceptionResponse(Exception ex,HttpServletResponse response) {
		//打印失败结果
		ex.printStackTrace();
		logger.error(ex);
		String message = ex.getMessage();
		if(ex instanceof ResultException){//数据异常统一类型resultExcpetion
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckError(ex.getMessage()));
		}else
		if(ex instanceof AjaxCheckError) {//自定义错误异常
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckError(ex.getMessage()));
		}else
		if(ex instanceof IotException) {//物联网套件异常
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckError(
							ex.getMessage(),
							((AjaxCheckError) ex).getMessage()));
		}else
		if(ex instanceof AjaxCheckSuccess) {//自定义成功异常
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckSuccess(ex.getMessage()));
		}else
		/*if(ex instanceof IncorrectCredentialsException) {//密码错误
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckError("账号或密码错误~"));
		}else
		if(ex instanceof UnknownAccountException) {//账号不存在
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckError("账号或密码错误~"));
		}else
		if(ex instanceof DisabledAccountException) {//账号被禁用
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckErrorBy401("账号已被禁用,请联系管理员~"));
		}else
		if(ex instanceof ExcessiveAttemptsException) {//登录次数过多
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckErrorBy401("登录失败次数过多~"));
		}else
		if(ex instanceof LockedAccountException) {//账号已被锁定
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckErrorBy401("账号已被锁定,请联系管理员~"));
		}else
		if(ex instanceof ExpiredCredentialsException) {//账号已过期
			
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckErrorBy401(StringUtils.isNotBlank(message)?message:"账号已过期~"));
		}else
		if(ex instanceof UnknownSessionException) {//登录失效
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckErrorBy401("登录已失效请重新登录~"));
		}else
		if(ex instanceof UserExpiredException) {//登录失效
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckErrorBy401("登录已失效请重新登录~"));
		}else
		if(ex instanceof UnauthorizedException) {//没有得到相应授权
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckErrorBy403("您没有权限访问~"));
		}else
		if(ex instanceof AuthorizationException) {//没有权限访问
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckErrorBy403("您没有权限访问~"));
		}else*/
		if (ex instanceof MaxUploadSizeExceededException) {//判断拦截的异常类型
			map = getMap();
			//返回状态码2 表示文件大小超过2M
			map.put("error", 2);
			ResponseUtil.returnJson(response,map);
		}else
		if(ex instanceof Exception){
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckError("系统繁忙~"));
		}else{
			ResponseUtil.returnJson(
					response, 
					ResponseUtil.ajaxCheckError("数据丢失了哦~"));
		}
	}
	
	
	private static Map<String, Object> getMap() {
		if(map == null) {
			map = new HashMap<>();
		}else
		if(!map.isEmpty()) {
			map.clear();
		}
		return map;
	}
}
